/*****************************************************\
* Copyright (c) 2020 The game2d Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.

* Author:         dzlua - https://gitee.com/dzlua/game2d.git
* Email:          505544956@qq.com
* FileName:       builder.h
* Create Time:    2020/03/21 18:13:51
* Description:    /
\****************************************************/
#ifndef __GAME2D_CORE_BUILDER_H__
#define __GAME2D_CORE_BUILDER_H__

#include "game2d.h"

namespace tinyxml2 {
  class XMLDocument;
  class XMLElement;
}
namespace g2d {
namespace ctrl {
  class UserInterface;
  class Control;
}
}

namespace g2d {
  GameObject* create_internal_object(const char *type, bool load_res_on_enter);

  class Builder {
    friend class Game2D;
    protected:
      Builder(Game2D *g2d);
      virtual ~Builder();
    protected:
      Result load_res_file(const char *xml);
      Result load_res_buffer(const char *xml, Uint32 len);
      Result load_scene_file(const char *xml);
      Result load_scene_buffer(const char *xml, Uint32 len);
    protected:
      Result parse_res(tinyxml2::XMLDocument *xml);
      Result parse_scene(tinyxml2::XMLDocument *xml);
      Result parse_res(tinyxml2::XMLElement *ele, Scene *scene);
      Result parse_scene(tinyxml2::XMLElement *ele);
      Result parse_layer(tinyxml2::XMLElement *ele, Scene *scene, bool load_res_on_enter);
      Result parse_sprite(tinyxml2::XMLElement *ele, Layer *layer, const char *type, bool load_res_on_enter);
      Result parse_ui(tinyxml2::XMLElement *ele, Layer *layer, bool load_res_on_enter);
      Result parse_ctrls(tinyxml2::XMLElement *ele, ctrl::UserInterface *ui, ctrl::Control *parent, bool load_res_on_enter);
    protected:
      Result parse_font(tinyxml2::XMLElement *ele, Font &font);
      Result parse_audio(tinyxml2::XMLElement *ele, Audio &audio);
      Result parse_texture(tinyxml2::XMLElement *ele, Texture &texture);
      Result parse_image(tinyxml2::XMLElement *ele);
    protected:
      Game2D *g2d_;
      std::map<std::string, TextureData> images_;
  };
} // end namespace g2d

#endif // __GAME2D_CORE_BUILDER_H__